package xcmg.device;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
@EnableKnife4j
public class Knife4jConfiguration {

    @Value("${swagger.enabled:true}")
    private boolean enabled;

    @Value("${swagger.groupName:dev}")
    private String groupName;

    @Value("${swagger.basePackage:xcmg.device.web}")
    private String basePackage;

    // 新增配置项：联系人信息和服务描述
    @Value("${swagger.contact.name:hanyun}")
    private String contactName;
    @Value("${swagger.contact.url:https://127.0.0.1:9213/doc.html}")
    private String contactUrl;
    @Value("${swagger.contact.email:dev@hanyun.com}")
    private String contactEmail;
    @Value("${swagger.title:WMS服务}")
    private String title;
    @Value("${swagger.description:WMS服务}")
    private String description;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.OAS_30)
                .enable(enabled)
                .groupName(groupName) // 启用分组配置
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .description(description) // 使用配置的描述
                .contact(new Contact(contactName, contactUrl, contactEmail)) // 使用配置的联系人
                .version("v1.0")
                .title(title) // 使用配置的标题
                .build();
    }
}
